﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OS
{
    static class Channel1
    {
        private static bool busyFlag = false;       // The boolean value for if Channel 1 is busy
        private static int timer = 0;               // The timer containing the number of cycles the channel has been busy
        private static int timeNeeded = 5;          // The time that Channel 1 takes to perform its task
        private static Buffer cachedBuffer = null;  // The cached buffer for the channel to operate on

        /// <summary>
        /// Getter/setter for busyFlag
        /// </summary>
        public static bool BusyFlag
        {
            get { return busyFlag; }
            set { busyFlag = value; }
        }

        /// <summary>
        /// Getter/setter for timer
        /// </summary>
        public static int Timer
        {
            get { return timer; }
            set { timer = value; }
        }

        /// <summary>
        /// Increments the timer
        /// </summary>
        public static void IncrementTimer()
        {
            timer++;
        }

        /// <summary>
        /// Resets the timer
        /// </summary>
        public static void ResetTimer()
        {
            timer = 0;
        }

        /// <summary>
        /// Getter/setter for timeNeeded
        /// </summary>
        public static int TimeNeeded
        {
            get { return timeNeeded; }
            private set { timeNeeded = value; }
        }

        /// <summary>
        /// Takes in a buffers and stores it until the channel is ready
        /// </summary>
        /// <param name="buffer">The buffer to cache</param>
        /*public static void CacheBuffer(Buffer buffer)
        {
            cachedBuffer = buffer;
        }*/

        /// <summary>
        /// Clears the cache
        /// </summary>
        public static void ClearCache()
        {
            cachedBuffer = null;
        }

        /// <summary>
        /// Channel 1's part of subroutine IR1
        /// </summary>
        public static Buffer StoreCardInBuffer(String line)
        {
            Buffer buffer = QueueManager.ebQueue.Dequeue();

            if (!buffer.Type.Equals(Buffer.TYPE.EMPTY))
            {
                Console.WriteLine("Channel 1 can only receive empty buffers.");
                return null;
            }
            else
            {
                Channel1.ClearCache();
                buffer.Data = line;
                buffer.Type = Buffer.TYPE.INPUTFUL;
                Trace.WriteToTrace("Empty buffer changed to inputful buffer.");
                return buffer;
            }
        }
    }
}